package com.example.everying.service.user.impl;/*
 * 项目名称: everything
 * 文件名: LabelServiceImpl
 * 作者: 张泽阳
 * 日期: 2025/5/23
 * 描述:
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.mapper.user.LabelMapper;
import com.example.everying.model.dto.user.EditLabelDto;
import com.example.everying.model.entity.user.LabelEntity;
import com.example.everying.model.entity.user.UserLabelRelEntity;
import com.example.everying.service.user.LabelService;
import com.example.everying.service.user.UserLabelRelService;
import com.example.everying.utils.RandomUtil;
import com.example.everying.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.everying.model.dto.user.LabelPageDto;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, LabelEntity> implements LabelService {
    @Autowired
    private UserLabelRelService userLabelRelService;


    @Override
    public IPage<LabelEntity> selectPage(IPage<LabelEntity> page, LabelPageDto labelPageDto) {
        LabelEntity labelEntity = new LabelEntity();
        labelEntity.setLabelName(labelPageDto.getLabelName());
        //BeanUtil.copyProperties(labelVo, labelEntity);
        QueryWrapper<LabelEntity> queryWrapper = new QueryWrapper<>();
        if (labelPageDto.getLabelName() != null) {
            queryWrapper.like("label_name", labelEntity.getLabelName());
        }
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByAsc("create_time");

        IPage<LabelEntity> pages = baseMapper.selectPage(page, queryWrapper);
        return pages;
    }

    @Override
    public List<LabelEntity> getLabelPicUrls(List<String> labelIds) {
        List<LabelEntity> labelEntities = listByIds(labelIds);
        // List<String> labelPicUrls = labelEntities.stream().map(LabelEntity::getLabelPicUrl).toList();
        return labelEntities;
    }

    @Override
    public Result<?> addLabel(EditLabelDto editLabelDto) {
        LabelEntity labelEntity = new LabelEntity();
        BeanUtil.copyProperties(editLabelDto, labelEntity);
        labelEntity.setId(RandomUtil.generateId());
        labelEntity.setCreateTime(LocalDateTime.now());
        labelEntity.setUpdateTime(LocalDateTime.now());
        int insert = baseMapper.insert(labelEntity);

        if (insert > 0){
            return Result.ok(baseMapper.selectById(labelEntity.getId()));
        }
        return Result.fail("插入失败");
    }

    @Override
    public Result<?> updateLabel(EditLabelDto editLabelDto) {
        List<UserLabelRelEntity> userLabelRelByLabelId = userLabelRelService.getUserLabelRelByLabelId(editLabelDto.getId());
        if (userLabelRelByLabelId != null && userLabelRelByLabelId.size() > 0){
            return Result.fail("标签关联用户不能删除！！！");
        }
        LabelEntity labelEntity = new LabelEntity();
        BeanUtil.copyProperties(editLabelDto, labelEntity);
        labelEntity.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(labelEntity);
        if(update > 0){
            return Result.ok(baseMapper.selectById(labelEntity.getId()));
        }
        return Result.fail("更新失败");
    }

    @Transactional
    @Override
    public Result<?> deleteLabelByIds(List<String> ids) {

        for (int i = 0; i < ids.size(); i++) {
            List<UserLabelRelEntity> userLabelRelEntities = userLabelRelService.getUserLabelRelByLabelId(ids.get(i));
            if (userLabelRelEntities != null && userLabelRelEntities.size() > 0){
                return Result.fail("标签关联用户不能删除！！！");
            }
            LabelEntity labelEntity = new LabelEntity();
            labelEntity.setId(ids.get(i));
            labelEntity.setIsDelete(1);
            labelEntity.setUpdateTime(LocalDateTime.now());
            boolean b = updateById(labelEntity);

            if (!b){
                return Result.fail("删除失败");
            }
        }

/*        List<LabelEntity> labelEntities = listByIds(ids);
        if (labelEntities == null){
            return Result.fail("删除失败");
        }
        for(LabelEntity labelEntity : labelEntities){
            labelEntity.setIsDelete(1);
        }
        boolean b = saveBatch(labelEntities);
        if (!b){
            return Result.fail("删除失败");
        }*/
        return Result.ok("删除成功");
    }

    @Override
    public Result<?> getLabelList() {
        LambdaQueryWrapper<LabelEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabelEntity::getIsDelete, 0);
        queryWrapper.eq(LabelEntity::getLabelStatus, 0);
        queryWrapper.orderByAsc(LabelEntity::getCreateTime);
        List<LabelEntity> labelEntities = baseMapper.selectList(queryWrapper);
        if (labelEntities != null){
            return Result.ok(labelEntities);
        }
        return Result.fail("未获取到数据");
    }


}
